package com.mengwen.backstage.util;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mengwen.backstage.config.WeChatConfig;
import com.mengwen.backstage.pojo.AccessToken;
import com.mengwen.backstage.pojo.UsersList;
import com.mengwen.backstage.pojo.WeixinUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author LiangYonghui
 * @date 2020/4/24 17:11
 * @description  根据传入的获取单个用户信息工具类
 */
@Component
@Slf4j
public class GetUserInfoUtil {

    @Autowired
    GetLastAccessTokenUtil getLastAccessTokenUtil;

    @Autowired
    WeChatConfig weChatConfig;


    /**
     * 获取前十
     * @param key
     * @param openid
     * @return
     * @throws Exception
     */
    public String getValueByKeyAndOpenid(String key,String openid) throws Exception {
        AccessToken accessToken = getLastAccessTokenUtil.getAccessToken();
        System.out.println(accessToken);
        String url = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=ACCESS_TOKEN";
        String access_token = url.replace("ACCESS_TOKEN", accessToken.getToken());
        HttpClientHelper httpClientHelper = new HttpClientHelper();
        String s = httpClientHelper.callTrustHttps1(access_token, "{\n" +
                "    \"user_list\": [\n" +
                "        {\n" +
                "            \"openid\": \"" + openid + "\", \n" +
                "            \"lang\": \"zh_CN\"\n" +
                "        }\n" +

                "    ]\n" +
                "}");

        JSONObject jsonObj = JSONObject.parseObject(s);
        String user_info_list = String.valueOf( jsonObj.get("user_info_list"));
        System.out.println(user_info_list);
        if (user_info_list.equals("null")) {
            log.info("获取用户信息为空，请核对openid。");
            return "";
        }
        String substring = user_info_list.substring(1);
        String substring1 = substring.substring(0, substring.length() - 1);
        log.info("用户信息为：" + substring1);
        JSONObject jsonObject = JSONObject.parseObject(substring1);
        String value = (String) jsonObject.get(key);
        return value;
    }





    public List<WeixinUser> getTenValueByKeyAndOpenid(String[] openids) throws Exception {
        log.info("===========openids==========");
        System.out.println(openids[0]);
        AccessToken accessToken = getLastAccessTokenUtil.getAccessToken();
        System.out.println(accessToken);
//        String url = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=ACCESS_TOKEN";
        String url = weChatConfig.getGetUserListInfoUrl();
        String access_token = url.replace("ACCESS_TOKEN", accessToken.getToken());
        HttpClientHelper httpClientHelper = new HttpClientHelper();

        // 自定义list中openid个数：
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("{\n" +
                "\t\"user_list\" : [");
        for (int z = 0; z < openids.length; z++) {
            if (!"".equals(openids[z])){
                stringBuilder.append("{\n" +
                        "\t\t \"openid\": \""+ openids[z] +"\",\n" +
                        "         \"lang\": \"zh_CN\"\n" +
                        "\t},");
            }
        }
        stringBuilder.replace(stringBuilder.length()-1,stringBuilder.length(),"");

        stringBuilder.append("]\n" +
                "}");


        String request_body = stringBuilder.toString();


        log.info("========request_body=========");
        System.out.println(request_body);

        String s = httpClientHelper.callTrustHttps1(access_token, request_body);

        JSONObject jsonObj = JSONObject.parseObject(s);
        System.out.println(jsonObj);
        String user_info_list = String.valueOf( jsonObj.get("user_info_list"));
        log.info("=============================用户信息====================================");
        log.info(user_info_list);
        List<WeixinUser> list = null;
        try {
            ObjectMapper objectMapper=new ObjectMapper();
            UsersList usersList = objectMapper.readValue(String.valueOf(jsonObj), UsersList.class);
            list = usersList.getUser_info_list();
            return list;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        String[] openids = {"ozV3UvoqDsYBIafI2mjz5GSkF-5k","ozV3UvoqDsYBIafI2mjz5GSkF-5k","",""};
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("{\n" +
                "\t\"user_list\" : [");
        for (int z = 0; z < openids.length; z++) {
            if (!"".equals(openids[z])){
                stringBuilder.append("{\n" +
                        "\t\t \"openid\": \"ozV3UvoqDsYBIafI2mjz5GSkF-5k\",\n" +
                        "         \"lang\": \"zh_CN\"\n" +
                        "\t},");
            }
        }
        stringBuilder.replace(stringBuilder.length()-1,stringBuilder.length(),"");

        stringBuilder.append("]\n" +
                "}");


        String string = stringBuilder.toString();

        System.out.println(string);
    }


}
